﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml.Serialization;
using System.IO;
using System.Collections;
using System.Xml;



namespace _3D_FileExplorer
{

    // Shopping list class which will be serialized
    [XmlRoot("ContentRating")]
    public class ContentRating
    {
        // This attribute enables the ArrayList to be serialized:
        [System.Xml.Serialization.XmlArray("Entry")]
        // Explicitly tell the serializer to expect the Item class
        // so it can be properly written to XML from the collection:
        [System.Xml.Serialization.XmlArrayItem("entry", typeof(REntry))]
        public ArrayList contRat;

        public ContentRating()
        {
            contRat = new ArrayList();
  
        }

        public int AddEntry(REntry entry)
        {
            return contRat.Add(entry);
        }

        public float GetRating(string type)
        {
            foreach (REntry en in contRat)
            {
                if (en.type == type)
                {
                    return en.value;
                }
            }

            float value = 1;
            return value;

        }


        public static void SerializeToXML(ContentRating cr)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ContentRating));
            TextWriter textWriter = new StreamWriter(Environment.CurrentDirectory + @"/XMLfiles/contentRating.xml");
            serializer.Serialize(textWriter, cr);
            textWriter.Close();

        }
        public static ContentRating DeserializeFromXML()
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(ContentRating));
            //TextReader textReader = new StreamReader(Fe.content.RootDirectory + @"\contentRating.xml");
            TextReader textReader = new StreamReader(Environment.CurrentDirectory + @"/XMLfiles/contentRating.xml");
            ContentRating cr;
            cr = (ContentRating)deserializer.Deserialize(textReader);
            textReader.Close();

            return cr;
        }
    }



    // Items in the shopping list
    public class REntry
    {
        [XmlAttribute("type")]
        public string type;
        [XmlAttribute("rating")]
        public float value;

        public REntry()
        {
        }

        public REntry(string type, float value)
        {
            this.type = type;
            this.value = value;
        }
    }
}
